package com.mk.plugin.function;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class DateRangeFunction extends Functions {

    public DateRangeFunction(String fieldName, String labelText) {
        super(fieldName);
        if (labelText != null) {
            this.setLabelText(labelText);
        }
    }

    private List<String> subviewProps = Arrays.asList(
            "searchCondition",
            String.format("%sPickerOpen", this.getFieldName()),
            String.format("open%sPicker", this.getFieldFirstUpperCamelCaseName()),
            String.format("close%sPicker", this.getFieldFirstUpperCamelCaseName()),
            String.format("tempStart%s", this.getFieldFirstUpperCamelCaseName()),
            String.format("tempEnd%s", this.getFieldFirstUpperCamelCaseName()),
            String.format("onChange%sRange", this.getFieldFirstUpperCamelCaseName()),
            String.format("onConfirm%sRange", this.getFieldFirstUpperCamelCaseName())
    );

    private List<String> needComponent = Arrays.asList(
            "MKToolbar",
            "MKToolbarLeftAction"
    );

    private List<String> needMapToProps = Arrays.asList(
            "searchCondition",
            String.format("%sPickerOpen", this.getFieldName()),
            String.format("tempStart%s", this.getFieldFirstUpperCamelCaseName()),
            String.format("tempEnd%s", this.getFieldFirstUpperCamelCaseName())
    );

    @Override
    public List<String> getSubviewProps() {
        return subviewProps;
    }

    @Override
    public List<String> getNeedComponent() {
        return needComponent;
    }

    @Override
    public List<String> getNeedMapToProps() {
        return needMapToProps;
    }

    @Override
    public String getLeftActionComponentString() {
        String result = String.format("<div className=\"date-range-container\" onClick={open%sPicker} ref={(r) => this.%sRangeBox = r}>\n", this.getFieldFirstUpperCamelCaseName(), this.getFieldName()) +
                "                            <div className=\"date-input\">\n" +
                "                                <input className=\"date-range\" readOnly={true}\n" +
                String.format("                                       value={searchCondition.start%s + \"~\" + searchCondition.end%s}/>\n", this.getFieldFirstUpperCamelCaseName(), this.getFieldFirstUpperCamelCaseName()) +
                "                                <i className=\"icon icon-calendar\"/>\n" +
                "                            </div>\n" +
                "                        </div>\n" +
                "\n" +
                "                        <MKDatePicker\n" +
                String.format("                            open={%sPickerOpen} anchorElement={this.%sRangeBox} anchorPosition=\"left\"\n", this.getFieldName(), this.getFieldName()) +
                String.format("                            style={{ width : \"560px\" }} startDate={tempStart%s} endDate={tempEnd%s}\n", this.getFieldFirstUpperCamelCaseName(), this.getFieldFirstUpperCamelCaseName()) +
                String.format("                            onChange={onChange%sRange} onConfirm={onConfirm%sRange} onRequestClose={close%sPicker} maskColor=\"rgba(0, 0, 0, 0.0)\"\n", this.getFieldFirstUpperCamelCaseName(), this.getFieldFirstUpperCamelCaseName(), this.getFieldFirstUpperCamelCaseName()) +
                "                        />";

        if (this.getLabelText() != null) {
            result = String.format("<div className=\"toolbar-label\">%s</div>\n", this.getLabelText()) + result;
        }
        return result;
    }

    @Override
    public String getSubviewStatement() {
        return "private " + this.getFieldName() + "RangeBox;";
    }

    @Override
    public String getActionString(Map<String, Object> properties) {
        return String.format("export function open%sPicker() {\n" +
                "    return {\n" +
                "        type : ActionType.OPEN_%s_PICKER_FOR_%s\n" +
                "    }\n" +
                "}\n" +
                "\n", this.getFieldFirstUpperCamelCaseName(), this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName")) +
                String.format("export function close%sPicker() {\n" +
                        "    return {\n" +
                        "        type : ActionType.CLOSE_%s_PICKER_FOR_%s\n" +
                        "    }\n" +
                        "}\n" +
                        "\n", this.getFieldFirstUpperCamelCaseName(), this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName")) +
                String.format("export function onChange%sRange(range) {\n" +
                        "    return {\n" +
                        "        type          : ActionType.ON_CHANGE_%s_RANGE_FOR_%s,\n" +
                        "        tempStart%s : range.startDate.format('YYYY-M-DD'),\n" +
                        "        tempEnd%s   : range.endDate.format('YYYY-M-DD')\n" +
                        "    }\n" +
                        "}\n" +
                        "\n", this.getFieldFirstUpperCamelCaseName(), this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"), this.getFieldFirstUpperCamelCaseName(), this.getFieldFirstUpperCamelCaseName()) +
                String.format("export function onConfirm%sRange() {\n" +
                        "    return {\n" +
                        "        type : ActionType.ON_CONFIRM_%s_RANGE_FOR_%s\n" +
                        "    }\n" +
                        "}\n" +
                        "\n", this.getFieldFirstUpperCamelCaseName(), this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"));
    }

    @Override
    public String getActionTypeString(Map<String, Object> properties) {
        return String.format("export const OPEN_%s_PICKER_FOR_%s = \"OPEN_%s_PICKER_FOR_%s\";\n" +
                        "export const CLOSE_%s_PICKER_FOR_%s = \"CLOSE_%s_PICKER_FOR_%s\";\n" +
                        "\n" +
                        "export const ON_CHANGE_%s_RANGE_FOR_%s = \"ON_CHANGE_%s_RANGE_FOR_%s\";\n" +
                        "\n" +
                        "export const ON_CONFIRM_%s_RANGE_FOR_%s = \"ON_CONFIRM_%s_RANGE_FOR_%s\";\n" +
                        "\n",
                this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"), this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"),
                this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"), this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"),
                this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"), this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"),
                this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"), this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"));
    }

    @Override
    public String getReducerSwitches(Map<String, Object> properties) {
        return String.format("case ActionType.OPEN_%s_PICKER_FOR_%s:\n" +
                        "            return handleOpen%sPickerFor%s(state);\n" +
                        "\n" +
                        "        case ActionType.CLOSE_%s_PICKER_FOR_%s:\n" +
                        "            return handleClose%sPickerFor%s(state);\n" +
                        "\n" +
                        "case ActionType.ON_CHANGE_%s_RANGE_FOR_%s:\n" +
                        "            return handleOnChange%sRangeFor%s(state, action);\n" +
                        "\n" +
                        "        case ActionType.ON_CONFIRM_%s_RANGE_FOR_%s:\n" +
                        "            return handleOnConfirm%sRangeFor%s(state);\n" +
                        "\n",
                this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"), this.getFieldFirstUpperCamelCaseName(), properties.get("UpperPageName"),
                this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"), this.getFieldFirstUpperCamelCaseName(), properties.get("UpperPageName"),
                this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"), this.getFieldFirstUpperCamelCaseName(), properties.get("UpperPageName"),
                this.getFieldAllUpperCaseUnderscoreName(), properties.get("allUpperCaseUnderscoreName"), this.getFieldFirstUpperCamelCaseName(), properties.get("UpperPageName"));
    }

    @Override
    public String getReducerFunctions(Map<String, Object> properties) {
        return String.format("function handleOpen%sPickerFor%s(state) {\n" +
                "    return Object.assign({}, state, {\n" +
                "        %sPickerOpen : true\n" +
                "    })\n" +
                "}\n" +
                "\n", this.getFieldFirstUpperCamelCaseName(), properties.get("UpperPageName"), this.getFieldName()) +
                String.format("function handleClose%sPickerFor%s(state) {\n" +
                        "    return Object.assign({}, state, {\n" +
                        "        %sPickerOpen : false\n" +
                        "    })\n" +
                        "}\n" +
                        "\n", this.getFieldFirstUpperCamelCaseName(), properties.get("UpperPageName"), this.getFieldName()) +
                String.format("function handleOnChange%sRangeFor%s(state, action) {\n" +
                        "    return Object.assign({}, state, {\n" +
                        "        tempStart%s : action.tempStart%s,\n" +
                        "        tempEnd%s   : action.tempEnd%s\n" +
                        "    })\n" +
                        "}\n" +
                        "\n", this.getFieldFirstUpperCamelCaseName(), properties.get("UpperPageName"), this.getFieldFirstUpperCamelCaseName(), this.getFieldFirstUpperCamelCaseName(), this.getFieldFirstUpperCamelCaseName(), this.getFieldFirstUpperCamelCaseName()) +
                String.format("function handleOnConfirm%sRangeFor%s(state) {\n" +
                        "    let searchCondition = Object.assign({}, state.searchCondition, {\n" +
                        "        start%s : state.tempStart%s,\n" +
                        "        end%s   : state.tempEnd%s\n" +
                        "    });\n" +
                        "\n", this.getFieldFirstUpperCamelCaseName(), properties.get("UpperPageName"), this.getFieldFirstUpperCamelCaseName(), this.getFieldFirstUpperCamelCaseName(), this.getFieldFirstUpperCamelCaseName(), this.getFieldFirstUpperCamelCaseName()) +
                "    return Object.assign({}, state, {\n" +
                "        searchCondition,\n" +
                String.format("        %sPickerOpen : false", this.getFieldName()) +
                "    })\n" +
                "}\n" +
                "\n";
    }

    @Override
    public List<String> getReducerInitState(Map<String, Object> properties) {
        return Arrays.asList(
                String.format("%sPickerOpen : false", this.getFieldName()),
                String.format("tempStart%s  : moment().add(-1, \"month\").format(\"L\")", this.getFieldFirstUpperCamelCaseName()),
                String.format("tempEnd%s    : moment().format(\"L\")", this.getFieldFirstUpperCamelCaseName())
        );
    }

    @Override
    public List<String> getSearchConditionInitState(Map<String, Object> properties) {
        return Arrays.asList(
                String.format("start%s : moment().add(-1, \"month\").format(\"L\")", this.getFieldFirstUpperCamelCaseName()),
                String.format("end%s   : moment().format(\"L\")", this.getFieldFirstUpperCamelCaseName())
        );
    }

    @Override
    public List<String> getMKComponentImport() {
        return Arrays.asList(
                "MKDatePicker"
        );
    }

    @Override
    public List<String> getReducerImportStatement() {
        return Arrays.asList(
                "import moment = require(\"moment\");"
        );
    }
}
